import * as layout from "./assets/js/layout.js";
import * as dom from "./assets/js/dom.js";
import * as db from "./assets/js/db.js";
import * as request from "./assets/js/request.js";
let storage = window.localStorage;
var ENABLE_SERVER = 1;
var ENABLE_UPDATE = 1;
//TODO
function postUpdate(elementId, message) {
  console.log("update");
}
function wsdb_article_submit(elementId, message) {
  if (ENABLE_UPDATE) {
    xml2article(elementId, message, postUpdate);
  } else {
    xml2article(elementId, message, null);
  }
}
function wsdb_article(tableName) {
  request.post_sql_form_with_callback("/sql",
    "select * from '" + tableName + "';",
    'article', layout.xml2article);
}
function wsdb_nav_ul_callback(elementId, message) {
  layout.xml2nav_ul(elementId, message, wsdb_article);
}
window.onload = function() {
  layout.standard();
  if (ENABLE_SERVER) {
    request.post_sql_form_with_callback("/sql",
      "select name from sqlite_master where type='table' order by name asc;",
      'ul', wsdb_nav_ul_callback);
    if (storage.tableName) {
      wsdb_article(storage.tableName);
    } else {
      request.post_sql_form_with_callback("/sql",
        "select name from sqlite_master where type='table' order by name asc;",
        'article', layout.xml2article);
    }
  } else {
    var test = '<dev dbName="/home/ts/Downloads/ts-3.1.0.db"> <dev name="1.device"/> <dev name="2.interface"/></dev>';
    layout.xml2nav_ul("ul", test, wsdb_article);
    test = '<dev><dev id="1" name="ss"/><dev id="2" name="2s"/></dev>';
    layout.xml2article("article", test, postUpdate);
    postUpdate('', "msg");
  }
};
